import { defineStore } from 'pinia'

const useUserStore = defineStore('User', {
  persist: true,
  state: () => {
    return {
      token: '',
      isLogin: false,
      userInfo: {
        exp: '',
        identity: '',
        nice: '',
        orig_iat: '',
        roleid: '',
        rolekey: '',
        rolename: '',
        tenantid: '',
      },
    }
  },
  getters: {
  
  },
  actions: {
    setUserInfo(info: any) {
      // 使用 $patch 来手动添加响应式属性
      this.$patch((state) => {
        state.userInfo = info;
      });
    },
    setToken(token: string) {
      this.$patch((state) => {
        state.token = token;
      });
    },
    clearToken() {
      this.$patch((state) => {
        state.token = '';
        state.isLogin = false
        state.userInfo = {
          exp: '',
          identity: '',
          nice: '',
          orig_iat: '',
          roleid: '',
          rolekey: '',
          rolename: '',
          tenantid: '',
        }
      });
    },
    setLoginState(isLogin: boolean) {
      this.$patch((state) => {
        state.isLogin = isLogin;
      });
    }
  },
})

export default useUserStore
